package com.goldenautumn.prefabricate.activity.mine.adapter

import android.content.Context
import android.view.MotionEvent
import android.view.View
import android.view.ViewConfiguration
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.core.content.ContextCompat
import androidx.core.view.ViewConfigurationCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.SizeUtils
import com.chad.library.adapter4.BaseQuickAdapter
import com.chad.library.adapter4.viewholder.DataBindingHolder
import com.goldenautumn.prefabricate.R
import com.goldenautumn.prefabricate.activity.cart.SettlementGoodsAdapter
import com.goldenautumn.prefabricate.activity.search.GoodsLayoutAdapter
import com.goldenautumn.prefabricate.business.entity.GoodsInfoVO
import com.goldenautumn.prefabricate.business.entity.GoodsSkuVo
import com.goldenautumn.prefabricate.business.entity.OrderInfoVo
import com.goldenautumn.prefabricate.databinding.ItemOrderItemBinding
import com.goldenautumn.prefabricate.utils.Constants.ORDER_TYPE_AFTER_SALES
import com.goldenautumn.prefabricate.utils.Constants.ORDER_TYPE_COMPLETE
import com.goldenautumn.prefabricate.utils.Constants.ORDER_TYPE_PENDING
import com.goldenautumn.prefabricate.utils.Constants.ORDER_TYPE_RECEIVING
import com.goldenautumn.prefabricate.utils.Constants.ORDER_TYPE_SHIPMENT
import com.goldenautumn.prefabricate.utils.loadImgUrl
import qncx.external.common.compat.gone
import qncx.external.common.compat.visible

class OrderItemAdapter  : BaseQuickAdapter<OrderInfoVo, DataBindingHolder<ItemOrderItemBinding>>() {

    override fun onBindViewHolder(holder: DataBindingHolder<ItemOrderItemBinding>, position: Int, item: OrderInfoVo?) {
        if(item == null) return
        var mBinding  = holder.binding
        var params = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        when (holder.layoutPosition) {
            items.size - 1 -> params.setMargins(
                0,
                SizeUtils.dp2px(8f),
                0,
                SizeUtils.dp2px(16f)
            )
            else -> params.setMargins(
                0,
                SizeUtils.dp2px(8f),
                0,
                0
            )
        }
        holder.itemView.layoutParams = params
        var goodsAdapter = SettlementGoodsAdapter()
        mBinding.goodsRv.layoutManager = LinearLayoutManager(context)
        mBinding.goodsRv.adapter = goodsAdapter

        mBinding.goodsRv.setOnTouchListener(object : View.OnTouchListener {

            override fun onTouch(v: View, event: MotionEvent): Boolean {
                if (event.action == MotionEvent.ACTION_UP) {
                    var scaledHorizontalScrollFactor = ViewConfigurationCompat.getScaledHorizontalScrollFactor(ViewConfiguration.get(context), context)
                    var scaledVerticalScrollFactor = ViewConfigurationCompat.getScaledVerticalScrollFactor(ViewConfiguration.get(context), context)
                    if (scaledHorizontalScrollFactor > 0 || scaledVerticalScrollFactor > 0) {
                        holder.itemView.performClick()
                        return true
                    }
                }
                return false
            }
        })

        var count = 0
        item?.goods?.forEach {
            count += (it.total_num ?: 0)
        }
        mBinding.totalCount.text = String.format(context.getString(R.string.unit),"${item.goods?.size ?: 0}")
        mBinding.totalPrice.text = String.format(context.getString(R.string.currency), "${item?.order_price}")
        mBinding.orderSn.text = String.format(context.getString(R.string.order_sn), "${item?.order_no}")
        var goods = ArrayList<GoodsInfoVO>()
        item.goods?.forEach {
            goods.add(GoodsInfoVO(
                goods_image = it.image?.file_path,
                goods_name = it.goods_name,
                goods_sku = GoodsSkuVo(goods_id = it.goods_id,goods_attr = it.goods_attr,goods_price = it?.goods_price, spec_sku_id = it?.spec_sku_id),
                total_num = it.total_num
            ))
        }
        goodsAdapter.submitList(goods)

        mBinding.actionBtnRight.setTextColor(ContextCompat.getColor(context,R.color.color_1D1F20))
        mBinding.actionBtnRight.shapeBuilder?.setShapeStrokeColor(ContextCompat.getColor(context,
            qncx.external.common.R.color.color_cccccc))?.into(mBinding.actionBtnRight)
        mBinding.actionBtnLeft.gone()
        when(item.order_status?.value) {
            "10" -> { //其他状态
                if(item.pay_status?.value == "20") { //已支付
                    if(item.delivery_status?.value == "10") { //待发货
                        mBinding.orderStatus.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(context, R.mipmap.ic_order_status_shipments),null, null, null)
                        mBinding.orderStatus.setTextColor(ContextCompat.getColor(context,R.color.color_F48A02))
                        mBinding.orderStatus.setText(R.string.order_status_shipment)
                        mBinding.actionBtnRight.setText(R.string.order_action_cancel)
                    } else { //已发货
                        mBinding.orderStatus.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(context, R.mipmap.ic_order_status_receiving),null, null, null)
                        mBinding.orderStatus.setTextColor(ContextCompat.getColor(context,R.color.colorPrimary))
                        mBinding.orderStatus.setText(R.string.order_status_receiving)
                        mBinding.actionBtnRight.setText(R.string.order_action_trace)
                        mBinding.actionBtnLeft.setText(R.string.order_status_after_sales)
                        mBinding.actionBtnLeft.visible()
                        mBinding.actionBtnRight.visible()
                        mBinding.actionBtnRight.setTextColor(ContextCompat.getColor(context,R.color.colorPrimary))
                        mBinding.actionBtnRight.shapeBuilder?.setShapeStrokeColor(ContextCompat.getColor(context,
                            qncx.external.common.R.color.colorPrimary))?.into(mBinding.actionBtnRight)
                    }
                } else {
                    mBinding.orderStatus.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(context, R.mipmap.ic_order_status_wait),null, null, null)
                    mBinding.orderStatus.setTextColor(ContextCompat.getColor(context,R.color.color_DE0000))
                    mBinding.orderStatus.setText(R.string.order_status_pending)
                    mBinding.actionBtnLeft.setText(R.string.order_action_cancel)
                    mBinding.actionBtnLeft.visible()
                    mBinding.actionBtnRight.visible()
                    mBinding.actionBtnRight.setText(R.string.order_action_pay)
                    mBinding.actionBtnRight.setTextColor(ContextCompat.getColor(context,R.color.color_BD3C37))
                    mBinding.actionBtnRight.shapeBuilder?.setShapeStrokeColor(ContextCompat.getColor(context,R.color.color_BD3C37))?.into(mBinding.actionBtnRight)
                }
           }
            "30" -> { //已完成
                mBinding.orderStatus.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(context, R.mipmap.ic_order_status_finish),null, null, null)
                mBinding.orderStatus.setTextColor(ContextCompat.getColor(context,R.color.colorPrimary))
                mBinding.orderStatus.setText(R.string.order_status_complete)
                mBinding.actionBtnRight.setText(R.string.order_action_repurchase)
            }
            "21","20" -> { //待取消(已付款)  以取消
                mBinding.orderStatus.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(context, R.mipmap.ic_order_status_cancel),null, null, null)
                mBinding.orderStatus.setTextColor(ContextCompat.getColor(context,R.color.color_1D1F20))
                mBinding.orderStatus.setText(R.string.order_status_after_cancel)
                mBinding.actionBtnRight.setText(R.string.order_action_repurchase)
            }
        }


    }

    override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): DataBindingHolder<ItemOrderItemBinding> {
        return DataBindingHolder(R.layout.item_order_item,parent)
    }

}